package cn.cloud.butler.webapi.controller;

import cn.cloud.butler.webapi.domain.PermissionEntity;
import cn.cloud.butler.webapi.domain.ResponseEntityNoData;
import cn.cloud.butler.webapi.domain.groupall.GroupAllResponseWithInteger;
import cn.cloud.butler.webapi.logger.WaLogger;
import cn.cloud.butler.webapi.service.ProducePriceService;
import cn.cloud.butler.webapi.service.PermissonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * author: daichaoren
 * time: 2019-01-07
 * description: 产品价格类接口
 */
@RestController
@RequestMapping(value = "/HncWeb")
public class ProducePriceController {
    //构造注入
    private final ProducePriceService producePriceService;
    private final PermissonService permissonService;
    @Autowired
    public ProducePriceController(ProducePriceService producePriceService, PermissonService permissonService) {
        this.producePriceService = producePriceService;
        this.permissonService = permissonService;
    }


    /**
     * 接口18
     * 查询机床厂所有的产品价格，每次查询机床厂生产的所有的工件，若没有价格，则设置为0
     * http://10.10.56.19:8080/HncWeb/SelCompProPrice.action?username=13163295907
     * @param username 用户名
     * @return GroupAllResponseWithInteger
     */
    @RequestMapping(method = RequestMethod.GET, value = "/SelCompProPrice.action")
    public GroupAllResponseWithInteger selCompProPrice(String username) {
        GroupAllResponseWithInteger result = new GroupAllResponseWithInteger();
            try {
                //从DB查询是否存在用户
                PermissionEntity permissons = permissonService.findPermissionByUser(username);
                List<HashMap<String,Object>> data = producePriceService.selCompProPrice(permissons.getCompID());
                result.setMsgCode(0);
                result.setMsgDesc("查询成功");
                result.setData(data);
            }catch (Exception e){
                result.setMsgCode(1);
                result.setMsgDesc("查询失败"+e.toString());
                WaLogger.logger.warn(e.toString());
            }
            return result;
    }

    /**
     * 接口 19
     * 添加机床用户厂工件价格
     * http://10.10.56.19:8080/HncWeb/insertPrice.action?username=zgc&producename=O00F12&price=800
     * @param username
     * @param producename
     * @param price
     * @return
     */
    @RequestMapping(value = "/insertPrice.action")
    public ResponseEntityNoData addPrice(String username, String producename, Float price) {
        ResponseEntityNoData response = new ResponseEntityNoData();
        try {
            Map<String, Object> map = producePriceService.addPrice(username, producename, price);
            response.setMsgCode(Integer.parseInt(map.get("MsgCode").toString()));
            response.setMsgDesc(map.get("MsgDesc").toString());
        }catch (Exception e){
            response.setMsgCode(1);
            response.setMsgDesc(e.toString());
            WaLogger.logger.warn(e.toString());
        }
        return  response;
    }
}